package com.zhangdi.demo.es.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhangdi.demo.es.entity.CompanyDoc;
import com.zhangdi.demo.es.entity.EngineerDoc;
import com.zhangdi.demo.es.esrepository.ESEngineerRepository;
import com.zhangdi.demo.es.esrepository.ESUserRepository;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Optional;

/**
 * Created by zhangdi on 2021/02/04 18:00:41.
 */

@Service
public class TestDoc {
    @Resource
    ESEngineerRepository engineerRepository;

    @Resource
    ESUserRepository companyRepository;

    @Resource
    RestHighLevelClient highLevelClient;

    public EngineerDoc findById(long id){
        Optional<EngineerDoc> optional =engineerRepository.findById(id);

        return optional.get();
    }

    public CompanyDoc insertCompany(CompanyDoc company){
        company.setMajorList(new ArrayList<>());
        company.setSlList(new ArrayList<>());
        company.setGlList(new ArrayList<>());
        company.setSkyList(new ArrayList<>());
        company.setZbList(new ArrayList<>());
        company.setEngineerList(new ArrayList<>());
//        company.setRecords(new ArrayList<>());
        company.setZzList(new ArrayList<>());
        return companyRepository.save(company);
    }

    public String insert(EngineerDoc newEr){
        Optional<EngineerDoc> optional = engineerRepository.findById(newEr.getId());
        if(optional.isPresent()){
            return update(newEr);
        }

        engineerRepository.save(newEr);

        return "success";
    }

    public String update(EngineerDoc newEr){
        Optional<EngineerDoc> optional = engineerRepository.findById(newEr.getId());
        EngineerDoc engineer;
        if(!optional.isPresent()){
            return "修改 没有找到";
        }
        engineer = optional.get();
        engineer.setId(newEr.getId());
        engineer.setName(newEr.getName());
        engineer.setCard(newEr.getCard());
        engineer.setCompanyId(newEr.getCompanyId());
        engineer.setCompanyName(newEr.getCompanyName());

        UpdateRequest updateRequest = new UpdateRequest("iowl_engineer",newEr.getId()+"");
        ObjectMapper mapper = new ObjectMapper();
        String json = null;
        try {
            json = mapper.writeValueAsString(newEr);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
//updateRequest.upsert(gson.toString(), XContentType.JSON);
        updateRequest.doc(json, XContentType.JSON);

        try {
            UpdateResponse response = highLevelClient.update(updateRequest, RequestOptions.DEFAULT);

        } catch (IOException e) {
            e.printStackTrace();
        }

        //engineerRepository.save(engineer);

        return "success";
    }
}
